<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户登录界面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<base href="${pageContext.request.contextPath}/">
<link type="text/css" rel="stylesheet" href="css/style.css;">
</head>
<body bgcolor="#E7ECEF">
	<center>
		<form id="log_Login_action" onsubmit="return false;" name="log_Login_action" method="post">
			<table border="0" cellspacing="0" cellpadding="0"
				style="margin-top: 130">
				<tr>
					<td><img src="images/logon_top.gif"></td>
				</tr>
				<tr height="180">
					<td background="images/logon_middle.gif" align="center"
						valign="top">
						<table border="0" width="90%" cellspacing="0" cellpadding="0">
							<tr height="50">
								<td colspan="2"></td>
							</tr>
							<tr height="30">
								<td align="right" width="40%">用户名：&nbsp;&nbsp;</td>
								<td style="text-indent: 5"><input type="text"
									name="name" size="30" value=""
									id="log_Login_action_user_userName" /> <br>
								<span style="color: red;" id="msg_n"></span></td>
							</tr>
							<tr height="30">
								<td align="right">密&nbsp;&nbsp;码：&nbsp;&nbsp;</td>
								<td style="text-indent: 5"><input type="password"
									name="pwd" size="30"
									id="log_Login_action_user_userPassword" /><br>
								<span style="color: red;" id="msg_p"></span></td>
							</tr>
							<tr height="30">
								<td align="right">验证码：&nbsp;&nbsp;</td>
								<td style="text-indent: 5;"><span> <input
										style="vertical-align: middle" type="text" name="yzcode"
										size="19" id="yzcode" /> <img style="vertical-align: middle"
										id="yzimg" alt="验证码图片" height="22" width="60" src="yzcode.jpg"
										onclick="refresh(this)" /> <script type="text/javascript">
											function refresh(obj) {
												obj.src = "yzcode.jpg?id="
														+ Math.random();
											}
										</script>
								</span><br>
								<span style="color: red;" id="msg_y"></span></td>
							</tr>
							<tr height="30">
								<td align="right"></td>
								<td style="text-indent: 5"><span style="font-size: small;"><input
										name="check"  id = "check" type="checkbox" value="" />是否保存登录状态</span></td>
							</tr>
							<tr height="60">
								<td></td>
								<td><input type="submit" id="" value="登录"
									onclick="return subDate();" /> <input type="reset" value="重置" />
									<a id="log_Login_action_" href="./">[返回首页]</a>
									<a id="log_Login_action_" href="register">[注册]</a></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td><img src="images/logon_end.gif"></td>
				</tr>
			</table>
		</form>
	</center>
	<script src="js/jquery.min.js"></script>
	<script src="js/check.js"></script>
	<script src="js/md5.js"></script>
	<script type="text/javascript">
		if(checkCookie()){
			document.getElementById("log_Login_action_user_userName").value = getCookie('username');
			document.getElementById("log_Login_action_user_userPassword").value = getCookie('password');
		}
		
		function subDate() {
			var name = $("#log_Login_action_user_userName").val();
			var pwd = $("#log_Login_action_user_userPassword").val();
			var yzcode = $("#yzcode").val();
			
			if (yzcode.length < 4) {
				$("#msg_y").html('验证码有误！');
				return false;
			} else {
				$("#msg_y").html('');
			}
			if (!isUserName(name)) {
				$("#msg_n").html('用户名只能有数字,字母,_，长度3-16！');
				return false;
			} else {
				$("#msg_n").html('');
			}
			if (pwd.length < 5) {
				$("#msg_p").html('密码太短，至少5位！');
				return false;
			} else {
				$("#msg_p").html('');
			}
			
			if(checkCookie()){
				if(name !=  getCookie('username') || pwd != getCookie('password')){
					cleanCookie('username', 'password');
					document.getElementById("log_Login_action_user_userPassword").value = hex_md5(pwd).toUpperCase();
				}
			}else{
				document.getElementById("log_Login_action_user_userPassword").value = hex_md5(pwd).toUpperCase();
			}
			
			name = $("#log_Login_action_user_userName").val();
			pwd = $("#log_Login_action_user_userPassword").val();
			$.ajax({
                cache: true,
                type: "POST",
                url:"./tologin",
                data:$('#log_Login_action').serialize(),
                async: false,
                error: function(request) {
                    alert("Connection error");
                },
                success: function(data) {
                	if (data.status - 200 == 0) {
    					$("#msg_y").html(data.msg);
    					if(document.getElementById("check").checked){
        					cleanCookie('username', 'password');
        					setCookie('username', trim(name), 'password', trim(pwd), 1);
        				}else{
        					cleanCookie('username', 'password');
        				}
    					location.reload();
    					return true;
    				} else {
    					$("#msg_y").html('');
    				}
                	
    				var timestamp = new Date().getTime();
    				$("#yzimg").attr("src", 'yzcode.jpg' + '?' + timestamp);
    				document.getElementById("log_Login_action_user_userPassword").value = '';
    				
    				if (data.status - 400 == 0) {
    					$("#msg_y").html(data.msg);
    					return false;
    				} else {
    					$("#msg_y").html('');
    				}

    				if (data.status - 1 == 0) {
    					$("#msg_n").html(data.msg);
    					return false;
    				} else {
    					$("#msg_n").html('');
    				}
    				if (data.status - 2 == 0) {
    					$("#msg_p").html(data.msg);
    					return false;
    				} else {
    					$("#msg_p").html('');
    				}

    				if (data.status - 300 == 0) {
    					$("#msg_n").html(data.msg);
    					return false;
    				} else {
    					$("#msg_n").html('');
    				}

    				if (data.status - 250 == 0) {
    					$("#msg_p").html(data.msg);
    					return false;
    				} else {
    					$("#msg_p").html('');
    				}
                }
            });
		}
		$("#log_Login_action_user_userName").focus();
	</script>

</body>
</html>